// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const db = cloud.database()
  const wxContext = cloud.getWXContext()
  if(event.level==3){
    var result=[];
    return await db.collection('scanCode')
    .where({
      brandidThree:event.brandid,
      openidThree: wxContext.OPENID,
    }).get()
    .then(res => {
      console.log(res)
      for(var i=0;i<res.data.length;i++){
        console.log(res.data[i])
        var item={
          device_logicid: res.data[i].device_logicid,
          isSubmit:res.data[i].isSubmitThree,
          scanCodeTime:res.data[i].scanCodeTimeThree,
        }
        result.push(item)
      }
      return result;
    })
  }else if(event.level==4){
    return await db.collection('scanCode')
    .where({
      brandidFour:event.brandid,
      openidFour: wxContext.OPENID,
    }).get()
    .then(res => {
      console.log(res)
      for(var i=0;i<res.data.length;i++){
        console.log(res.data[i])
        var item={
          device_logicid: res.data[i].device_logicid,
          isSubmit:res.data[i].isSubmitFour,
          scanCodeTime:res.data[i].scanCodeTimeFour,
        }
        result.push(item)
      }
      return result;
    })
  }else if(event.level==5){
    return await db.collection('scanCode')
    .where({
      brandidFive:event.brandid,
      openidFive: wxContext.OPENID,
    }).get()
    .then(res => {
      console.log(res)
      for(var i=0;i<res.data.length;i++){
        console.log(res.data[i])
        var item={
          device_logicid: res.data[i].device_logicid,
          isSubmit:res.data[i].isSubmitFive,
          scanCodeTime:res.data[i].scanCodeTimeFive,
        }
        result.push(item)
      }
      return result;
    })
  }else{
    return -1
  }
}